/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.felix.servicediagnostics.impl

// this import of implicit methods makes java to scala collections conversions transparent (lists, maps)
import scala.collection.JavaConversions._

import org.osgi.framework.BundleContext
import org.osgi.framework.ServiceReference

import org.apache.felix.dm.DependencyManager
import org.apache.felix.dm.Component
import org.apache.felix.dm.ComponentDeclaration
import org.apache.felix.dm.ComponentDeclaration._
import org.apache.felix.dm.ComponentDependencyDeclaration
import org.apache.felix.dm.ComponentDependencyDeclaration._

import org.apache.felix.servicediagnostics._

/**
 * This class is the ServiceDiagnosticsPlugin implementation for org.apache.felix.dm.DependencyManager.
 *
 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
 */
class DMNotAvail(val bc:BundleContext) extends ServiceDiagnosticsPlugin 
{
    override def components:List[Comp] = 
    {
        // this involves a bit of type casting gymnastics because the underlying 
        // API does not use generic types and parsing of strings because the 
        // underlying API does not provide accessors
        (for {
            dm <- DependencyManager.getDependencyManagers.map(_.asInstanceOf[DependencyManager])
            comp <- dm.getComponents.map(_.asInstanceOf[Component])
            compdec = comp.asInstanceOf[ComponentDeclaration]
            impl = comp.toString.split(" ").toList.last.takeWhile(_ != ']').trim
            service <- compdec.getName.takeWhile(_ != '(').split(",") //multiple services for one comp
            deps = compdec.getComponentDependencies
                          .map(dep => new Dependency(dep.getName.takeWhile(_ != '(').trim, 
                                           dep.getName.dropWhile(_ != '(').trim match {
                                               case "" => None
                                               case f => Some(f)
                                           },
                                           dep.getState != STATE_UNAVAILABLE_REQUIRED)).toList
          }
            // yield Comp builds a list of Comp out of the for comprehension
            yield new Comp(impl,
                           service.trim, 
                           comp.getServiceProperties,
                           (compdec.getState != STATE_UNREGISTERED),
                           deps)) toList 
    }
}
